<?php

class Core_Backend_Component_Global_Controller_Area extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20) {
        $this->getHistory()->record();
        $collection = App::getModel('global/collection_area')
                        ->pagination($pageindex, $pagesize)
                        ->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function createAction() {
        return $this->viewResult();
    }

    public function editAction($id) {
        $model = App::getModel('global/area')->load($id);
        return $this->viewResult(array('model' => $model));
    }

    public function applyPostAction() {
        return $this->saveModel(FALSE);
    }

    public function savePostAction() {
        return $this->saveModel();
    }

    private function saveModel($return = TRUE) {
        $data = $this->getRequest()->getForm('area');
        $model = App::getModel('global/area');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data);
        if (($errors = $model->validate()) === TRUE) {
            $model->save();
            $this->getMessager()->addInform($this->__f('share.message.saveSuccess', '{global.label.area}', $model->getName()));
            return $return ?
                    $this->redirectResult($this->getHistoryUrl('global/area/index')):
                    $this->redirectResult($this->getUrl(array('id' => $model->getIdValue()), 'edit'));
                    
        } else {
            $this->getMessager()
                 ->addError($this->__f('share.message.saveFail', '{global.label.area}', $model->getName()))
                 ->addError($errors);
            return $this->viewResult(array('model' => $model));
        }
    }

}